<?php
	
	
	//setcookie("username", $_POST["username"]);
?>
<?php
	$redirect = "loginform.php";
	
	$username = trim($_POST["username"]);
	$password = trim($_POST["password"]);
	$vpassword = trim($_POST["vpassword"]);
	$gender = trim($_POST["sex"]);
	$zipcode = trim($_POST["zipcode"]);
	$age = trim($_POST["age"]);
	$occupation = trim($_POST["occupation"]);
	setcookie("username", $username, time()+120);
	setcookie("gender", $gender, time()+120);
	setcookie("vpassword", $vpassword, time()+120);
	setcookie("password", $password, time()+120);
	setcookie("zipcode", $zipcode, time()+120);
	setcookie("age", $age, time()+120);
	setcookie("occupation", $occupation, time()+120);
	if( $password != $vpassword)
	{
		$redirect="wiki.php";
		//echo( "password does not match");
	}

	header( "refresh:15;url=$redirect" );
	if( !$username || !$password || !$vpassword || !$gender || !$zipcode || !$age || !$occupation )
		$redirect = "create_user.php";
	if($password != $vpassword )
		die("password does not match");
	
	if( $username )
		echo "<br> username: $username";
	else
	{
		echo "<br> no username selected";

	}
	if( $password ) 
		echo "<br> password: $password";
	if( $vpassword )
		echo "<br> vpassword: $vpassword";
	
	if( $gender )
		echo "<br> gender: $gender ";
	else if( strlen($zipcode) != 5 ) 
		die("invalid zipcode");
	else
		die("<br> no gender selected");
	
	if( $zipcode ) 
		echo "<br> zipcode: $zipcode ";
	else
		die("<br> You must enter a zipcode.");
	
	if( $age ) 
		echo "<br> age: $age";
	else
		die("<br> You must enter an age.");
	
	if( $occupation )
		echo "<br> occupation: $occupation";
	else
		die("<br> You must select an occupation");
	
if( $username )
{
	$connection = mysql_connect("localhost","root","howdy123") or exit("cannot connect to database");
	if( $connection )
		echo '<br> connection successful <br>';
	$db = mysql_select_db("db",$connection);
	
	echo "<br> username: ". $username ."<br>";
	$query = "SELECT * FROM user WHERE username = '$username'";
	$statement = mysql_query($query,$connection);

	if( $statement )
		echo "mysql query was valid. <br>";
	else
		echo "sql error: ". mysql_error(). "<br>";
	
	$results = mysql_num_rows($statement);

	if( $results <= 0)
		die("invalid username or password");

	$user_info = mysql_fetch_array($statement);

	echo "username: ". $user_info["username"] ."<br>";
	echo "user_id: ". $user_info["user_id"] ."<br>";
	echo "gender: ". $user_info["gender"] ."<br>";
	
	$query = "INSERT INTO
			user(age, gender,occupation, zipcode, username,password) 
			VALUES (". $age .",'". $gender ."','". $occupation ."',". $zipcode .",'". $username ."','". $password ."')";
	
	$statement = mysql_query($query,$connection);

	if( $statement ) 
		echo "mysql query was valid <br>";
	else
		die( "sql error: ". mysql_error(). "<br>");
	
	


	if( $connection )
		echo "You are connected <br>";
	else
		die( "Invalid login");

	
	$connection = mysql_close($connection);

}

?>

	
